package app.map;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.MapView;

import mainUI.AppActivity;
import menhoo.com.i8officelibrary.R;
import tools.PermissionManger;
import tools.ToastUtil;


/**
 * AMapV2地图中介绍如何显示一个基本地图
 */
public class BasicMapActivity extends AppActivity implements BasicMapView,OnClickListener{

	private BasicMapPersenter persenter = new BasicMapPersenter(this);
	private MapView mapView;
	private TextView txt_sure;
	private TextView txt_addresL;
	private TextView txt_addresS;
	private ImageView btn_location;
	private RelativeLayout layout_search;
	private String title = "";

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PermissionManger.PERMISSIONS) {
            for(int i = 0; i < grantResults.length; i++){
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED){

                }else{
                    Toast.makeText(this, "请开启权限后再使用", Toast.LENGTH_LONG).show();
                    finish();
                    return;
                }
            }
        }
    }
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

        PermissionManger.getPermission(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION});

        setContentView(R.layout.basicmap_activity);
		txt_sure = (TextView)findViewById(R.id.txt_sure);
		txt_sure.setOnClickListener(this);
		txt_addresL = (TextView)findViewById(R.id.txt_addresL);
		txt_addresL.setMovementMethod(ScrollingMovementMethod.getInstance());
		txt_addresS = (TextView)findViewById(R.id.txt_addresS);
		btn_location = (ImageView)findViewById(R.id.btn_location);
		btn_location.setOnClickListener(this);
		mapView = (MapView) findViewById(R.id.map);
		layout_search = (RelativeLayout)findViewById(R.id.layout_search);
		layout_search.setOnClickListener(this);

		try {
			title = getIntent().getStringExtra("title");
		}catch (Exception e){
		}
		if(TextUtils.isEmpty(title) || title.equalsIgnoreCase("null")){
			title = "定位";
		}

		setTitle(title);

		mapView.onCreate(savedInstanceState);// 此方法必须重写
		persenter.initLocation(this.getApplicationContext());
		persenter.initMap(this,mapView.getMap());

		persenter.startLocation();

	}


	@Override
	protected void onResume() {
		super.onResume();
		mapView.onResume();
	}


	@Override
	protected void onPause() {
		super.onPause();
		mapView.onPause();
	}


	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		mapView.onDestroy();
		persenter.stopLocation();
		persenter.destroyLocation();
	}


	public static final int GET_ADDRES_NAME = 2;
	@Override
	public void onClick(View v) {
		int i = v.getId();
		if (i == R.id.layout_search) {
			Intent it = new Intent(BasicMapActivity.this, InputtipsActivity.class);
			it.putExtra("city", persenter.getCity());
			it.putExtra("title", title);
			startActivityForResult(it,GET_ADDRES_NAME);
		}else if (i == R.id.btn_location){
			persenter.stopLocation();
			persenter.startLocation();
		}else if (i == R.id.txt_sure){
			Intent it = new Intent();
			it.putExtra("locationFullName", persenter.getLongName());
			it.putExtra("name", persenter.getShotname());
			it.putExtra("gps_longitude", persenter.getLongitude());
			it.putExtra("gps_latitude", persenter.getLatitude());
			setResult(Activity.RESULT_OK, it);
			finish();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == GET_ADDRES_NAME){
			if(resultCode == Activity.RESULT_OK){
				String LaddressName = data.getStringExtra("LaddressName");
				String SaddressName = data.getStringExtra("SaddressName");
				persenter.setAddressName(LaddressName,SaddressName);
				persenter.getLatlon(LaddressName);
			}
		}
	}

	//--------------------View实现-----------------
	@Override
	public void showToast(String text){
		ToastUtil.show(BasicMapActivity.this, text);
	}
	@Override
	public void showToast(int  rCode){
		ToastUtil.showerror(this, rCode);
	}

	@Override
	public void showAddressName(String LaddressName, String SaddressName) {
		txt_addresL.setText(LaddressName);
		txt_addresS.setText(SaddressName);
	}

}